#include "callconnect.h"

CallConnect::CallConnect(QObject *parent) : QObject(parent)
{

}

void CallConnect::slot_initCallConnect()
{
    if (m_tcpServer == nullptr)
        m_tcpServer = new QTcpServer();
    if (!m_tcpServer->listen(QHostAddress::Any, 8122)) {
        qDebug() << "监听失败";
        return;
    }

    connect(m_tcpServer, &QTcpServer::newConnection, this, &CallConnect::slot_newConnection);
}

void CallConnect::slot_newConnection()
{
    if (m_tcpServer->hasPendingConnections())
        m_tcpSocket = m_tcpServer->nextPendingConnection();

    connect(m_tcpSocket, &QTcpSocket::readyRead, this, &CallConnect::slot_readData);

    qDebug() << QString("[%1:%2]连接成功")
                .arg(m_tcpSocket->peerAddress().toString())
                .arg(m_tcpSocket->peerPort());
}

void CallConnect::slot_readData()
{
    QByteArray data = m_tcpSocket->read(1024);
    int dataType = analysisData(data);
}

int CallConnect::analysisData(const QByteArray &data)
{
    auto list = data.split('^');
    qDebug() << data;

    if (list[0] == "#CALLREQUEST") {

    }
}
